﻿
//DataTable对象,DataSouece一个Json对象
function DataTable(dataSouece)
{
    this.DataSouece = dataSouece;
    
    this.Rows = new Array();

    for (var i = 0; i < this.DataSouece.length; i++) {

        this.Rows.push(new DataRow(this.DataSouece[i]));
    }
};
//获取到指定行
DataTable.prototype.Row=function(index)
{
    

    var row = this.Rows[index];
    return row;
}

//获取到行的总数
DataTable.prototype.RowCount=function()
{

    return this.Rows.length;
}
//添加新行
DataTable.prototype.Insert=function(row)
{
    var json = [];

    var index = 0;
    for (var i = 0; i < this.DataSouece.length; i++) {

        index ++;
        json.push(this.DataSouece[i]);

    }

    if (row.rowResult[0]) {
        json.push(row.rowResult[0]);
    
    this.DataSouece = json;
    
  
    this.Rows.push(new DataRow(this.DataSouece[index]));
   
  }
   
   
}
//根据索引添加行
DataTable.prototype.InsertAt = function (index,row) {
    var json = [];
    this.Rows=new Array();
    
    for (var i = 0; i < this.DataSouece.length; i++) {

        if (index == i) {
            if (row.rowResult[0]) {
                json.push(row.rowResult[0]);
            }
            this.Rows.push(row);
        }
        json.push(this.DataSouece[i]);
        this.Rows.push(new DataRow(this.DataSouece[i]));
    }
    this.DataSouece = json;

}

//删除指定行
DataTable.prototype.Remove = function (index)
{

    var json = [];
    this.Rows = new Array();
    for (var i = 0; i < this.DataSouece.length; i++) {
        if (index != i) {
            json.push(this.DataSouece[i]);
            this.Rows.push(new DataRow(this.DataSouece[i]));

        }
    }
    
    this.DataSouece = json;
    

}

//DataRow对象
function DataRow(rowResult)
{
    

    
   
    this.rowResult = rowResult;


    if (rowResult)
    {
        this.Cells = new DataCoulumn(rowResult);
    }
   
    this.array = new Array();
}

//设置列的值
DataRow.prototype.Insert = function (columnName, value)
{

    
    this.array.push(columnName, value);

    
    var item = "";
    for (var i = 0; i <this.array.length; i+=2) {
     
        if (i == (this.array.length - 2)) {

            if (isNaN(this.array[i + 1])) {
                item += "'" + this.array[i] + "':'" + this.array[i + 1] + "'";
            }
            else {
                item += "'" + this.array[i] + "':" + this.array[i + 1];

            }
        }
        else {
            if (isNaN(this.array[i + 1]))
            {
                item += "'" + this.array[i] + "':'" + this.array[i + 1] + "',";
            } else
            {
 
                item += "'" + this.array[i] + "':" + this.array[i + 1] + ",";

            }
        }
    }

   
   var newRowResult=[];
    eval('var obj={' + item + '}');
  
    newRowResult.push(obj);
  
    this.rowResult = newRowResult;
    return this.rowResult;

}
//获取到指定列
DataRow.prototype.GetValue = function (columnName) {

    if (this.rowResult[columnName]) {
        return new DataCoulumn(this.rowResult[columnName]);
    } else if (this.rowResult[0][columnName]) {

        return new DataCoulumn(this.rowResult[0][columnName]);
    }


}

//设置列的值
DataRow.prototype.SetValue=function(columnName,value)
{
    if (this.rowResult[columnName]) {
       
        this.rowResult[columnName] = value;
    } else if (this.rowResult[0][columnName]) {

        this.rowResult[0][columnName] = value;
    }
}

//获取到列的值
DataRow.prototype.GetValue=function(columnName)
{
    if (this.rowResult[columnName]) {

       return this.rowResult[columnName]
    } else if (this.rowResult[0][columnName]) {

       return  this.rowResult[0][columnName]
    }

}
//DataColumn列对象
function DataCoulumn(value)
{

    this.value = value;
 
}

//获取到列的值
DataCoulumn.prototype.GetValue = function (columnName) {
    return this.value;
}
